<?php
/**
 * Created by PhpStorm.
 * User: lyw
 * Date: 2016/5/31
 * Time: 10:30
 */

class Push_informationController extends BaseController {

    public function index(){
        $doInformation = new Do咨询信息();
        $count = $doInformation->count();
        $page = new \tools\Page($count,15);
        $informations = $doInformation->offset($page->limit)
            ->limit($page->pagesize)
            ->orderby('id desc')
            ->rows();

        require $this->template;
    }

    //添加模板咨询
    public function add_information(){
        $doInformation = new Do咨询信息();
        $id = $this->request('id');
        if($id){
            $doInformation->set_id($id);
            $information = $doInformation->where($doInformation->data)->row();
            $info = $information->data;
        }
        require $this->template;
    }

    //处理模板咨询
    public function add_information_action(){
        $doInformation = new Do咨询信息();
        $id = $this->request('id');
        $page = $this->request('page');
        $msg = '添加';
        if($id){
            $msg = '修改';
            $doInformation->set_id($id);
        }else{
            global $G_CONFIG;
            if(!$G_CONFIG['模板消息ID']['咨询模板']){
                echo "<script>alert('咨询模板ID不能为空');</script>";
                return;
            }
            $doInformation->set_咨询模板ID(trim($G_CONFIG['模板消息ID']['咨询模板']));
        }
        $doInformation->set_标题($this->request('title','',true));
        $doInformation->set_发布人($this->request('owner'));
        $doInformation->set_消息类别($this->request('type'));
        $doInformation->set_发布时间($this->request('time'));
        $doInformation->set_追加($this->request('additional'));
        $doInformation->set_添加时间(date("Y-m-d H:i:s"));
        if($doInformation->save()){
            if($id){
                $url = $this->url('push_information','index',array('page'=>$page));
            }else{
                $infoId = $doInformation->get_id();
                $url = $this->url('push_information','information_detail',array('id'=>$infoId));
            }
            $this->successPage($msg.'成功',$url);
        }else{
            $this->errorPage($msg.'失败');
        }
    }

    //显示图文消息
    public function information_detail(){
        $infoId = $this->request('id');
        $doDetail = new Do咨询详情();
        $doDetail->set_咨询id($infoId);
        $details = $doDetail->where($doDetail->data)->orderby('排序')->rows();
        $news_type=array('普通咨询','课程回顾');
        require $this->template;
    }

    //添加或修改图文消息
    public function add_image_message(){
        $doDetail = new Do咨询详情();
        $id = $this->request('detail_id');
        $infoId = $this->request('info_id');
        if($id){
            $doDetail->set_id($id);
        }else{
            $doDetail->set_添加时间(date("Y-m-d H:i:s",time()));
        }
        $doDetail->set_标题($this->request('title','',true));
        $doDetail->set_文章url($this->request('text_url','',true));
        $doDetail->set_description($this->request('description'));
        $doDetail->set_排序($this->request('sort'));
        $doDetail->set_news_type($this->request('news_type'));
        $doDetail->set_咨询id($infoId);
        if($_FILES['pic']['name']){
            $path='../cache/msg_img';
            if(is_dir($path)){
                mkdir($path,"0777");
            }
            $img=Tools::File_Upload($_FILES['pic'],$path);
            $doDetail->set_图片url($img);
        }
        if($doDetail->save()){
            $url = $this->url('push_information','information_detail',array('id'=>$infoId));
            echo "<script>alert('操作成功!');location.href='".$url."'</script>";
        }else{
            echo "<script>alert('操作失败！');</script>";
        }
    }

    //显示单图文消息
    public function show_image_message(){
        $doDetail = new Do咨询详情();
        $id = $this->request('detail_id','',true);
        $detail = $doDetail->row($id);
        echo json_encode($detail->data);
    }

    //删除图文消息
    public function del(){
        $id = $this->request('detail_id');
        $infoId = $this->request('info_id');
        $doDetail = new Do咨询详情();
        $doDetail->set_id($id);
        $res = $doDetail->delete();
        if($res){
            $url = $this->url('push_information','information_detail',array('id'=>$infoId));
            echo "<script>alert('删除成功！');location.href='".$url."';</script>";
        }
    }

    //推送资讯消息
    public function push_message(){
        $bm = new C发微信消息();
        $infoId = $this->request('id','',true);
        $openId = $this->request('oid');

        $doInformation = new Do咨询信息();
        $doInformation->set_id($infoId);
        $doInformation->set_发送('已发送');

        $doDetail = new Do咨询详情();
        $doDetail->set_咨询id($infoId);
        $res = $doDetail->where($doDetail->data)->count();

        if($res>0){
            if (!$doInformation->where($doInformation->data)->count() and !$openId) {
                $doInformation->save();
            }

            if ($openId) {
                $msg = '测试通过';
                $bm->ff发送咨询信息($infoId,$openId);
            } else {
                $msg = '后台促发完毕';
                $this->run_cli('zixun_msg', $infoId);
            }

            $this->jsonOutPut(0, $msg);
        }else{
            $this->jsonOutPut(-1, '请添加图文消息');
        }

    }

    //给未收到消息的用户重推
    public function accept_failed_user(){
        $bm = new C发微信消息();
        $id = $this->request('id');
        $bm->ff发送咨询信息($id,'','失败');
        echo true;
    }



}